package com.jtpay.textpay.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.jtpay.textpay.AppidDataProvider;
import com.jtpay.textpay.PayBean;
import com.jtpay.textpay.R;
import com.jtpay.textpay.adapter.MyAdapter;
import com.jtpay.textpay.util.SharedPreferenceUtil;

import java.util.List;

import static com.jtpay.textpay.TextApplication.getSharedPreferenceUtil;

public class AppidListActivity extends AppCompatActivity {
    private ListView listView;
    private MyAdapter mAdapter;
    private List<PayBean> list;
    private SharedPreferenceUtil sharedPreferenceUtil = getSharedPreferenceUtil();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appid_list);
        listView = findViewById(R.id.appid_list);
        list = AppidDataProvider.getListWithoutReport(this);
        if (list == null || list.size() < 1) {
            return;
        }
        mAdapter = new MyAdapter(this, list);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PayBean bean = list.get(position);
                String appid = bean.getAppid();
                String appKey = bean.getAppKey();
                String aesKey = bean.getAesKey();
                String aesV = bean.getAesV();
                sharedPreferenceUtil.spSave(AppidListActivity.this,"appid",appid);
                sharedPreferenceUtil.spSave(AppidListActivity.this,"appKey",appKey);
                sharedPreferenceUtil.spSave(AppidListActivity.this,"aesKey",aesKey);
                sharedPreferenceUtil.spSave(AppidListActivity.this,"aesV",aesV);
                finish();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                PayBean bean = list.get(position);
                String appid = bean.getAppid();
                startDialog(position,appid);
                return true;
            }
        });
    }

    private void startDialog(final int position, final String appid) {
        AlertDialog.Builder builder = new AlertDialog.Builder(AppidListActivity.this);
        builder.setTitle("提示")
                .setMessage("确定删除吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        list.remove(position);
                        mAdapter.notifyDataSetChanged();
                        AppidDataProvider.deleteDB(AppidListActivity.this,appid);
                    }
                })
                .create()
                .show();
    }
}
